

def tree_menu(data, pk=None):
    """
        递归生成树形结构
    """
    tree_list = []

    if data:
        menu = data.filter(parent=pk)

        if menu:
            for (index, items) in enumerate(menu):
                menu_item = items
                menu_item['children'] = tree_menu(data, items['id'])
                tree_list.append(menu_item)

        return tree_list
    else:
        return tree_list

